<?php
namespace app\admin\controller;
use think\Db;
use app\common\controller\Backend;
class Bankuai extends Backend{
	public function lists(){
        $parent=input('get.parent');
        $where=array();
        $where['parent']=$parent?$parent:0;
        $list=Db::table('member_plate')->where($where)->paginate(12);
        $this->assign('list',$list);
		return $this->view->fetch();
	}


	public function edits(){
        $id=input('get.id');
        $data=array();
        if($id){
            $data=Db::table('member_plate')->where('id', $id)->find();
        }
        $parents=Db::table('member_plate')->select();
        $parents=getTree($parents,0,0);
        $this->assign('data',$data);
        $this->assign('parents',$parents);
        return $this->view->fetch();
	}

    public function save(){
        $data=$this->request->post();
        if(empty($data['id'])){
            $data['number']=date('YmdHis');
            $re=Db::table('member_plate')->insert($data);
        }else{
            $re=Db::table('member_plate')->where('id', $data['id'])->update($data);
        }
        if($re!==false){
            $this->success(__('编辑成功'), 'Bankuai/lists');
        }else{
            $this->success(__('编辑失败'), 'Bankuai/lists');
        }
    }


    public function delete(){
        $id=input('get.id');
        $re=Db::table('member_plate')->where('id',$id)->delete();
        $re2=Db::table('member_plate')->where('parent',$id)->delete();
        if($re && $re2){
            $this->success(__('删除成功'), 'Bankuai/lists');
        }else{
            $this->success(__('删除失败'), 'Bankuai/lists');
        }
    }


   //页面管理
	public function ylist(){
        $plate_id=input('get.plate_id');
        $where=array();
        if($plate_id){
            $plate=Db::table('member_plate')->where(['id'=>$plate_id])->find();
            if($plate['parent']==0){
                $child_plate=Db::table('member_plate')->where(['parent'=>$plate_id])->select();
                $chid_id= array_column($child_plate, 'id');
                array_push($chid_id,intval($plate_id));
                $where['plate_id']=array("in",$chid_id);
            }else{
                $where['plate_id']=$plate_id;
            }

        }
        $list=Db::table('member_page')->where($where)->paginate(12,false,['query'=>request()->param() ]);
        $this->assign('list',$list);
        $this->assign('plate_id',$plate_id);
		return $this->view->fetch();
	}

   
	public function yedit(){
        $id=input('get.id');
        $plate_id=input('get.plate_id');
        //应用类型
        $app_type= Db::table('member_app_type')->select();
        //板块
        $plate=Db::table('member_plate')->select();

        $plate_tree=getTree($plate,0,0);

        $data=array();
        if($id){
          $data=Db::table('member_page')->where('id',$id)->find();
          !empty($data)?$data['type_id']=explode(',', $data['type_id']):'';
        }
        $this->assign('app_type',$app_type);
        $this->assign('plate',$plate_tree);
        $this->assign('data',$data);
        $this->assign('plate_id',$plate_id);
		return $this->view->fetch();
	}

     public function ysave(){
        $data=$this->request->post();
         if(empty($data['type_id'])){
             $this->success(__('请选择应用类型'), 'Bankuai/ylist');
         }
        $type_id=$data['type_id'];
         /*foreach ( $data['type_id'] as $type){

         }

         die();*/
        $data['type_id'] =isset($data['type_id'])?implode(',', $data['type_id']):[];

        if(empty($data['id'])){
            $data['number']='p'.date('YmdHis');
            $re=Db::table('member_page')->insert($data);
        }else{
            $re=Db::table('member_page')->where('id', $data['id'])->update($data);
            //如果应用类型变了，那么对应的应用类型下的功能也该取消
            $page_gongnen=Db::table('member_plate_gongnen')->where('page_id', $data['id'])->select();
            foreach ($page_gongnen as $value){
                $data_ch=db('gongnen_child')->where('id', $value['child_id'])->find();
                $data_ch['type_id']=explode(",",$data_ch['type_id']);
                //获取两个数组相同的元素
                $result=array_intersect($type_id,$data_ch['type_id']);
                //如果没有，则删除这个功能
                if(empty($result)){
                    Db::table('member_plate_gongnen')->where('id', $value['id'])->delete();
                }
            }
        }
        if($re!==false){
            $this->success(__('编辑成功'), 'Bankuai/ylist');
        }else{
            $this->success(__('编辑失败'), 'Bankuai/ylist');
        }
    } 


    public function ydelete(){
        $id=input('get.id');
        $re=Db::table('member_page')->where('id',$id)->delete();
        if($re){
            $this->success(__('删除成功'), 'Bankuai/ylist');
        }else{
            $this->success(__('删除失败'), 'Bankuai/ylist');
        }
    }




    //功能列表
	public function glist(){
        $page_id=input('get.page_id');
        $where=array();
        if($page_id){
            $where['page_id']=$page_id;
        }
        $list=Db::table('member_plate_gongnen')->where($where)->paginate(12);
        $this->assign('page_id',$page_id);
        $this->assign('list',$list);
		return $this->view->fetch();
	}
	public function gedit(){
        if(!input('get.page_id')){
             $this->error(__('请在页面中添加功能'), 'Bankuai/ylist');
        }
        $page_id=input('get.page_id');

        $models=$this->getTypeGongNen($page_id);

        $gongnen=$models[0]['child'];
        $child=$gongnen[0]['child'];

        $id=input('get.id');
        $data=array();
      
        $this->assign('gongnen',$gongnen);
        $this->assign('child',$child);
        $this->assign('models',$models);
        $this->assign('data',$data);
        $this->assign('page_id',input('get.page_id'));
		return $this->view->fetch();
	}


	private function getTypeGongNen($page_id){
        $page=Db::table('member_page')->where('id',$page_id)->find();

        $page['type_id']=empty($page['type_id'])?[]:explode(",",$page['type_id']);

        $child_gongneng=[];
        $child=[];
        foreach ($page['type_id'] as &$value){
            $child_gongneng[$value]=Db::table('member_gongnen_child')
                ->where('type_id','like',"%{$value}%")
                ->select();
            foreach ($child_gongneng[$value] as $child_value){
                $child[$child_value['id']]=$child_value;
            }
        }
        //重置索引
        $child=array_values($child);

        //根据子功能获取功能
        $child_id=array_column($child,'gid');
        $gongnen=Db::table('member_gongnen')->where(array('id'=>array('in',$child_id)))->select();

        //根据功能获取模块
        $model_id=array_column($gongnen,'model_id');

        $models=Db::table('member_model_manage')->where(array('id'=>array('in',$model_id)))->select();


        foreach ($gongnen as &$g) {
           $g['child']=[];
          foreach ($child as $c) {

              if ($c['gid'] == $g['id']) {
                  $g['child'][]=$c;
              }
          }
       }

        foreach ($models as &$m) {
            $m['child']=[];
            foreach ($gongnen as $g) {

                if ($m['id'] == $g['model_id']) {
                    $m['child'][]=$g;
                }
            }
        }

       return $models;

    }


    public function gsave(){
         $data=$this->request->post();
        
        if(!empty($data['child_id'])){
                foreach ($data['child_id'] as $key => $value) {
                     $info[$key]['page_id']=$data['page_id'];
                     $info[$key]['gid']=$data['gid'];
                     $info[$key]['model_id']=$data['model_id'];
                     $info[$key]['child_id']=$value;
                }
       }
        $re=Db::table('member_plate_gongnen')->insertAll($info);
        if($re){
            $this->success(__('编辑成功'), 'Bankuai/glist?page_id='.$data['page_id']);
        }else{
            $this->success(__('编辑失败'), 'Bankuai/glist?page_id='.$data['page_id']);
        }
    }

    public function gdelete(){
        $id=input('get.id');
        $re=Db::table('member_plate_gongnen')->where('id',$id)->delete();
        if($re){
            $this->success(__('删除成功'), 'Bankuai/glist');
        }else{
            $this->success(__('删除失败'), 'Bankuai/glist');
        }
    }



    public function ajaxGetGongNen(){
        $model_id=input('post.model_id');
        $page_id=input('post.page_id');

        $list=[];
        $child=[];
        $models=$this->getTypeGongNen($page_id);
        foreach ($models as $v){
            if($v['id']==$model_id){
                $list=$v['child'];
                $child=$list[0]['child'];
            }
        }

        $html='';
        $child_html='';
        foreach ($list as $key => $value) {
            $html.='<option value="'.$value['id'].'">'.$value['name'].'</option>';
        }

        foreach ($child as $key => $value) {
            $child_html.='<label style="font-weight: normal;margin-right: 15px;cursor: pointer;">'.$value['cname'].'<input style="position: relative;top: 2px;" name="child_id[]" value="'.$value['id'].'" type="checkbox"></label>';
        }

        $this->success(['html'=>$html,'child_html'=>$child_html], '');
    }

    public function ajaxGetChild(){
         $page_id=input('post.page_id');
         $model_id=input('post.model_id');
         $gid=input('post.gid');
         $models=$this->getTypeGongNen($page_id);
         $models=array_column($models,null,'id');
         $gongnen=$models[$model_id]['child'];
         $gongnen=array_column($gongnen,null,'id');
         $child=$gongnen[$gid]['child'];

         $html='';
         foreach ($child as $key => $value) {
             $html.='<label style="font-weight: normal;margin-right: 15px;cursor: pointer;">'.$value['cname'].'<input style="position: relative;top: 2px;" name="child_id[]" value="'.$value['id'].'" type="checkbox"></label>';
         }
        $this->success($html, '');
    }


    //功能的技术选型
    public function gongjishu(){
        $gid=input('get.gid');
        $gongnen= Db::table('member_plate_gongnen')->where('id',$gid)->find();

        $page=Db::table('member_page')->where('id',$gongnen['page_id'])->find();

        $page['type_id']=empty($page['type_id'])?[]:explode(",",$page['type_id']);

        //应用类型
        $app_type= Db::table('member_app_type')->where(array('id'=>array("in",$page['type_id'])))->select();
        $map['gid']=$gid;
        $map['is_model']=0;
        foreach ($app_type as $key => &$value) {
            $map['type_id']=$value['id'];
            $value['list']=Db::table('member_technology_select')->where($map)->find();
            !empty($value['list'])?$value['list']['technology']=json_decode($value['list']['technology'],true):'';
        }


        //时间预算列表
        $budget_manage= Db::table('member_budget_manage')->select();

        //技术语言
        $technology_language=Db::table('member_technology_language')->select();

        $this->assign('app_type',$app_type);
        $this->assign('gongnen',$gongnen);
        $this->assign('budget_manage',$budget_manage);
        $this->assign('technology_language',$technology_language);
        return $this->view->fetch();
    }


    public function gongjsave(){
        $data=$this->request->post();

        if(empty($data['id'])){
            $data['is_model']=0;
            $re=Db::table('member_technology_select')->insert($data);
        }else{
            $re=Db::table('member_technology_select')->where('id', $data['id'])->update($data);
        }
        if($re!==false){
            $this->success(__('编辑成功'), 'Bankuai/glist');
        }else{
            $this->success(__('编辑失败'), 'Bankuai/glist');
        }
    }



    //页面的技术选型
	public function jishu(){
        $page_id=input('get.page_id');

        $page=Db::table('member_page')->where('id',$page_id)->find();

        $page['type_id']=empty($page['type_id'])?[]:explode(",",$page['type_id']);


        $app_type= Db::table('member_app_type')->where(array('id'=>array("in",$page['type_id'])))->select();

        //应用类型
        foreach ($app_type as $key => &$value) {
            $type_child_gongnen=Db::table('member_gongnen_child')
                ->where('type_id','like',"%{$value['id']}%")
                ->field('id,cname,gid,type_id,status')
                ->select();
            $chid_ids= array_column($type_child_gongnen, 'id');
            $value['gongnens']=Db::table('member_plate_gongnen')->where(array('page_id'=>$page_id,'child_id'=>['in',$chid_ids]))->select();

            $value['list_2']=Db::table('member_bankuai_technology_select')->where(array('page_id'=>$page_id,'type_id'=>$value['id'],'tech_type'=>2))->select();
            $value['list_3']=Db::table('member_bankuai_technology_select')->where(array('page_id'=>$page_id,'type_id'=>$value['id'],'tech_type'=>3))->select();
        }

        $app_version=Db::table('member_app_version')->select();

        //时间预算列表
        //功能的
        $budget_cate1=Db::table('member_budget_cate')->where('name','功能')->find();
        $budget_manage1= Db::table('member_budget_manage')->where('cate_id',$budget_cate1['id'])->select();

        //页面制作的
        $budget_cate2=Db::table('member_budget_cate')->where('name','页面制作')->find();
        $budget_manage2= Db::table('member_budget_manage')->where('cate_id',$budget_cate2['id'])->select();

        //页面设计的
        $budget_cate3=Db::table('member_budget_cate')->where('name','页面设计')->find();
        $budget_manage3= Db::table('member_budget_manage')->where('cate_id',$budget_cate3['id'])->select();


        //应用语言
        //功能的
        $app_cate1=Db::table('member_app_category')->where(['name'=>'功能开发','develop_model'=>1])->find();
        $language1= Db::table('member_app_language')->where('cate_id',$app_cate1['id'])->select();

        //页面制作的
        $app_cate2=Db::table('member_app_category')->where(['name'=>'前端开发','develop_model'=>1])->find();
        $language2= Db::table('member_app_language')->where('cate_id',$app_cate2['id'])->select();

        //页面设计的
        $app_cate3=Db::table('member_app_category')->where(['name'=>'ui设计','develop_model'=>1])->find();
        $language3= Db::table('member_app_language')->where('cate_id',$app_cate3['id'])->select();


        $this->assign('app_type',$app_type);
        $this->assign('page_id',$page_id);
        $this->assign('budget_manage1',$budget_manage1);
        $this->assign('budget_manage2',$budget_manage2);
        $this->assign('budget_manage3',$budget_manage3);
        $this->assign('technology_language1',$language1);
        $this->assign('technology_language2',$language2);
        $this->assign('technology_language3',$language3);
        $this->assign('app_version',$app_version);
		return $this->view->fetch();
	}


    public function jsave(){
        $datas=$this->request->post();
        $datas['arrs']=json_decode($datas['arrs'],true);

        foreach ($datas['arrs'] as $data){
            if(empty($data['id'])){
                $re=Db::table('member_bankuai_technology_select')->insert($data);
            }else{
                $re=Db::table('member_bankuai_technology_select')->where('id', $data['id'])->update($data);
            }
        }

        if($re!==false){
            $this->success(__('编辑成功'), 'ylist');
        }else{
            $this->success(__('编辑失败'), 'ylist');
        }
    }
}